package cn.edu.nju.software.cdiy.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.BRD_CHA;
import cn.edu.nju.software.cdiy.bean.Brand;
import cn.edu.nju.software.cdiy.bean.Cha_Interface;
import cn.edu.nju.software.cdiy.bean.Cha_MBArchitect;
import cn.edu.nju.software.cdiy.bean.Chassis;
import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.bean.HardwareSet;
import cn.edu.nju.software.cdiy.bean.Interface;
import cn.edu.nju.software.cdiy.bean.MainBoard;
import cn.edu.nju.software.cdiy.bean.Value;
import cn.edu.nju.software.cdiy.dao.ChassisDao;
import cn.edu.nju.software.cdiy.util.Changer;
import cn.edu.nju.software.cdiy.util.Constant;
import cn.edu.nju.software.cdiy.util.FilterItem;
import cn.edu.nju.software.cdiy.util.Item;
import cn.edu.nju.software.cdiy.util.Pager;
import cn.edu.nju.software.cdiy.util.SortByHotAsc;
import cn.edu.nju.software.cdiy.util.SortType;
import cn.edu.nju.software.cdiy.util.SortWay;

public class ChassisDaoImpl implements ChassisDao {
	
	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<Chassis> getChaList(int page, SortType st, HardwareSet hs,
			SortWay sw) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = null;
		List<Chassis> list = null;

		// 第page页的第minNum条记录到maxNum条记录
		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		switch (st) {
		case PRICE:
			if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
				sql = "FROM Chassis ORDER BY OPRICE ASC WHERE STATE=882";
			else
				sql = "FROM Chassis ORDER BY OPRICE DESC WHERE STATE=882";
			list = sess.createQuery(sql).list();
			break;
		case PERFORMANCE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Chassis ORDER BY PERFORMANCE DESC WHERE STATE=882";
			else
				sql = "FROM Chassis ORDER BY PERFORMANCE ASC WHERE STATE=882";
			list = sess.createQuery(sql).list();
			break;
		case W_SCORE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Chassis ORDER BY WSCORE DESC WHERE STATE=882";
			else
				sql = "FROM Chassis ORDER BY WSCORE ASC WHERE STATE=882";
			list = sess.createQuery(sql).list();
			break;
		case Z_SCORE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Chassis ORDER BY ZSCORE DESC WHERE STATE=882";
			else
				sql = "FROM Chassis ORDER BY ZSCORE ASC WHERE STATE=882";
			list = sess.createQuery(sql).list();
			break;
		case NEW:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Chassis ORDER BY INSTORAGETIME DESC WHERE STATE=882";
			else
				sql = "FROM Chassis ORDER BY INSTORAGETIME ASC WHERE STATE=882";
			list = sess.createQuery(sql).list();

			break;
		case HOT:

			sql = "FROM Chassis WHERE STATE=882";
			list = sess.createQuery(sql).list();
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				Collections.sort(list);
			else {
				Collections.sort(list, new SortByHotAsc());
			}
			break;
		default:
			sql = "FROM Chassis WHERE STATE=882";
			list = sess.createQuery(sql).list();
			break;

		}

		list = filterList(hs, list, minNum, maxNum);
		
		return list;
	}

	private List<Chassis> filterList(HardwareSet hs, List<Chassis> list,
			int minNum, int maxNum) {
		/*
		 * 主板对机箱的限制
		 */
//		list = uniFilter(hs, list);

		List<Chassis> returnList = new Pager<Chassis>().getPageList(list,
				minNum, maxNum);
		return returnList;
	}

	private List<Chassis> MBFilter(HardwareSet hs, List<Chassis> l) {
		/*
		 * 主板对机箱的限制 主板板型
		 */
		if (hs.getMainBoard() != null) {
			// 获取主板的板型
			MainBoard mb = hs.getMainBoard();
			int mbArchitect = mb.getMBArchitect().getKey();
			Session sess = sessionFactory.getCurrentSession();
			// 返回的list
			List<Chassis> returnList = new ArrayList<Chassis>();

			for (int i = 0; i < l.size(); i++) {
				// 遍历原表进行筛选
				Chassis nowCH = l.get(i);
				int id = nowCH.getId();
				String sql = "FROM Cha_MBArchitect WHERE Cha=" + id;
				List<Cha_MBArchitect> list = sess.createQuery(sql).list();

				for (int j = 0; j < list.size(); j++) {
					// 检测此Chassis是否支持这个主板的板型
					if (list.get(j).getMbArchitect().getKey() == mbArchitect) {
						// 若支持这个系列 则加入返回list 并跳出进行下一个CHA的检验
						returnList.add(nowCH);
					} else
						continue;
				}
			}
			
			return returnList;
		}
		return l;

	}

	@Override
	public List<Hardware> getNewestList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Chassis WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
		List<Chassis> list = sess.createQuery(sql).list();
		if (list.size() > Constant.NEW_LIST_LENGTH)
			list = list.subList(0, Constant.NEW_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);
		
		return returnList;
	}

	@Override
	public List<Hardware> getHottestList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Chassis WHERE STATE=882";
		List<Chassis> list = sess.createQuery(sql).list();

		Collections.sort(list);

		if (list.size() > Constant.HOT_LIST_LENGTH)
			list = list.subList(0, Constant.HOT_LIST_LENGTH);

		List<Hardware> returnList = Changer.toHardwareList(list);
		
		return returnList;
	}

	@Override
	public List<Brand> getBrandList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM BRD_CHA";
		List<Brand> returnList = new ArrayList<Brand>();
		List<BRD_CHA> list = sess.createQuery(sql).list();

		for (int i = 0; i < list.size(); i++) {
			returnList.add(list.get(i));
		}

		
		return returnList;
	}

	@Override
	public List<Chassis> filter(int page, Map<String, List<Integer>> map,
			SortType st, SortWay sw, HardwareSet hs) {
		// map为null 无筛选条件 直接调用匹配方法
		// 则直接根据hs获得list
		// 筛选和组装时通用
		if (map==null || map.size()==0)
			return getChaList(page, st, hs, sw);

		// map不为null
		// 获得此产品表
		List<Chassis> hwList = getFilterList(hs, st, sw);
		

		// 最后的匹配表
		List<Chassis> matchList = new ArrayList<Chassis>();

		// 开始过滤
		Set<String> keys = map.keySet();
		int i = 0;
		for (String s : keys) {
			i++;
			if (s.equals(Constant.chaItemNameList[0])) {
				// 品牌过滤
				List<Integer> list = map.get(s);
				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getBrand().getBrand()
								.equals(getBrandById(list.get(k)).getBrand())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}

			} else if (s.equals(Constant.chaItemNameList[1])) {
				// 价格过滤
				List<Integer> list = map.get(s);
				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer.checkPrice(hwList.get(j),
								Constant.CHA_PRICE[list.get(k) - 1].getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}

				}

			} else if (s.equals(Constant.chaItemNameList[2])) {
				// 机箱类型
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getType().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}

			} else if (s.equals(Constant.chaItemNameList[3])) {
				// 机箱结构
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getStructure().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}

			}
			if (i != keys.size()) {
				hwList = matchList;
				matchList = new ArrayList<Chassis>();
			}
		}
		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);
		matchList = new Pager<Chassis>().getPageList(hwList, minNum, maxNum);
		return matchList;
	}

	private Value getValueByID(Integer integer) {
		Session sess = sessionFactory.getCurrentSession();
		Value c = (Value) sess.load(Value.class, integer);
		
		return c;
	}

	private BRD_CHA getBrandById(Integer integer) {
		Session sess = sessionFactory.getCurrentSession();
		BRD_CHA c = (BRD_CHA) sess.load(BRD_CHA.class, integer);
		
		return c;
	}

	private List<Chassis> getFilterList(HardwareSet hs, SortType st, SortWay sw) {
		/*
		 * 获取用于筛选的硬件列表 此列表可以由action提供 或由本方法从数据库获取
		 */
		List<Chassis> l = new ArrayList<Chassis>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = null;

		switch (st) {
		case PRICE:

			if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
				sql = "FROM Chassis WHERE STATE=882 ORDER BY OPRICE ASC";
			else
				sql = "FROM Chassis WHERE STATE=882 ORDER BY OPRICE DESC";
			l = sess.createQuery(sql).list();

			break;
		case W_SCORE:

			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Chassis WHERE STATE=882 ORDER BY WSCORE DESC";
			else
				sql = "FROM Chassis WHERE STATE=882 ORDER BY WSCORE ASC";
			l = sess.createQuery(sql).list();

			break;
		case Z_SCORE:

			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Chassis WHERE STATE=882 ORDER BY ZSCORE DESC";
			else
				sql = "FROM Chassis WHERE STATE=882 ORDER BY ZSCORE ASC";
			l = sess.createQuery(sql).list();

			break;
		case NEW:

			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Chassis WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
			else
				sql = "FROM Chassis WHERE STATE=882 ORDER BY INSTORAGETIME ASC";
			l = sess.createQuery(sql).list();

			break;
		case HOT:

			sql = "FROM Chassis WHERE STATE=882";
			l = sess.createQuery(sql).list();
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				Collections.sort(l);
			else {
				Collections.sort(l, new SortByHotAsc());
			}

			break;
		default:
			break;

		}
		// 若hs为空 则为正常筛选
		// 若hs不为空 则为组装时的筛选
		l = uniFilter(hs, l);
		
		return l;
	}

	@Override
	public List<Chassis> search(String keywords, int page) {
		Session sess = sessionFactory.getCurrentSession();

		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		String sql = "FROM Chassis WHERE STATE=882";
		String keys[] = keywords.split(" ");
		for (int i = 0; i < keys.length; i++) {
			if (i == 0) {
				sql = sql + " WHERE MODEL LIKE '%" + keys[i] + "%'";
			} else {
				sql = sql + " OR MODEL LIKE '%" + keys[i] + "%'";
			}
		}
		List<Chassis> list = sess.createQuery(sql).list();

		List<Chassis> returnList = new Pager<Chassis>().getPageList(list,
				minNum, maxNum);

		
		return returnList;
	}

	@Override
	public HashMap<String, List<Item>> getFilterBoard() {
		HashMap<String, List<Item>> map = new HashMap<String, List<Item>>();
		map.put(getBrandItem().getItemName(), getFilterBrandItem());
		map.put(getPriceItem().getItemName(), getFilterPriceItem());

		for (int i = 2; i < Constant.chaItemNameList.length; i++) {
			map.put(Constant.chaItemNameList[i], getFilterItemByNum(i));
		}

		return map;
	}

	private List<Item> getFilterItemByNum(int i) {
		Session sess = sessionFactory.getCurrentSession();

		String sql = null;
		List<Value> values = null;
		List<Item> choices = null;

		switch (i) {
		case 2:
			// 机箱类型

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=676 AND KEY<=690 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		case 3:
			// 机箱结构

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=691 AND KEY<=700 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		default:
			break;

		}

		
		return choices;
	}

	private List<Item> getFilterPriceItem() {
		List<Item> choices = new ArrayList<Item>();
		for (int i = 0; i < (Constant.CHA_PRICE.length); i++) {
			choices.add(Constant.CHA_PRICE[i]);
		}
		return choices;
	}

	private List<Item> getFilterBrandItem() {
		Session sess = sessionFactory.getCurrentSession();

		String sql = "FROM BRD_CHA";
		List<BRD_CHA> brandList = sess.createQuery(sql).list();
		List<Item> choices = new ArrayList<Item>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(new Item(brandList.get(i).getBrand(), brandList.get(i)
					.getId()));
		}

		
		return choices;
	}

	private FilterItem getItemByNum(int i) {
		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		// itemName
		fi.setItemName(Constant.chaItemNameList[i]);

		String sql = null;
		List<Value> values = null;
		List<String> choices = null;

		switch (i) {
		case 2:
			// 机箱类型

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=676 AND KEY<=690 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		case 3:
			// 机箱结构

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=691 AND KEY<=700 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		default:
			break;

		}
		fi.setItemChoiceList(choices);
		
		return fi;
	}

	private FilterItem getPriceItem() {
		// 价格项
		FilterItem fi = new FilterItem();
		fi.setItemName(Constant.chaItemNameList[1]);

		List<String> choices = new ArrayList<String>();
		for (int i = 0; i < (Constant.CHA_PRICE.length); i++) {
			choices.add(Constant.CHA_PRICE[i].getName());
		}

		fi.setItemChoiceList(choices);

		return fi;
	}

	private FilterItem getBrandItem() {
		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		fi.setItemName(Constant.chaItemNameList[0]);

		String sql = "FROM BRD_CHA";
		List<BRD_CHA> brandList = sess.createQuery(sql).list();
		List<String> choices = new ArrayList<String>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(brandList.get(i).getBrand());
		}

		fi.setItemChoiceList(choices);
		
		return fi;
	}

	@Override
	public Chassis getOneById(int id) {
		Session sess = sessionFactory.getCurrentSession();
		Chassis c = (Chassis) sess.load(Chassis.class, id);
		
		return c;
	}

	@Override
	public Map<String, List<String>> getInsertboard() {
		// 返回的map
		Map<String, List<String>> rMap = new HashMap<String, List<String>>();

		Session sess = sessionFactory.getCurrentSession();
		String sql = null;
		String s = null;
		List<String> l = null;

		// 品牌
		s = Constant.chaSelectStr[0];
		sql = "FROM BRD_CHA";
		l = new ArrayList<String>();
		List<BRD_CHA> brandList = sess.createQuery(sql).list();
		for (int i = 0; i < brandList.size(); i++) {
			l.add(brandList.get(i).getBrand());
		}
		rMap.put(s, l);

		// 机箱类型和机箱结构
		rMap.put(Constant.chaSelectStr[1], getValueByKey(676, 690));

		// 机箱结构
		rMap.put(Constant.chaSelectStr[2], getValueByKey(691, 700));

		// 机箱材质
		rMap.put(Constant.chaSelectStr[3], getValueByKey(701, 710));

		// 理线功能
		rMap.put(Constant.chaSelectStr[4], getValueByKey(711, 713));

		// 一对多
		// 机箱接口
		l = new ArrayList<String>();
		sql = "FROM Interface WHERE RE_RAD=1 ORDER BY INTF_TYPE,NAME";
		List<Interface> list = sess.createQuery(sql).list();
		for (int i = 0; i < list.size(); i++) {
			l.add(list.get(i).getName());
		}
		rMap.put(Constant.chaComboBoxStr[0], l);

		// 支持主板架构
		l = new ArrayList<String>();
		sql = "FROM Value WHERE KEY>=81 AND KEY<=90 ORDER BY VALUE";
		List<Value> list1 = sess.createQuery(sql).list();
		for (int i = 0; i < list1.size(); i++) {
			l.add(list1.get(i).getValue());
		}
		rMap.put(Constant.chaComboBoxStr[1], l);

		
		return rMap;
	}

	@Override
	public boolean insertHardware(Map<String, List<String>> map) {
		try {
			// TODO Auto-generated method stub
			Session sess = sessionFactory.getCurrentSession();
			Chassis cha = new Chassis();
			// 本方法处理外键和专属属性，通用属性由其他方法提供
			// Chassis有一对多的情况
			Iterator i = map.keySet().iterator();
			// 一对多的list
			List<String> chaIntf = null;
			List<String> chaMBA = null;
			while (i.hasNext()) {
				String key = i.next().toString();
				List<String> list = map.get(key);
				String sql = null;
				if (key.equals(Constant.chaInforStr[0])) {
					cha.setModel(list.get(0));
					// 外键
				} else if (key.equals(Constant.chaSelectStr[0])) {
					sql = "FROM BRD_CHA WHERE BRAND='" + list.get(0) + "'";
					List<BRD_CHA> bl = sess.createQuery(sql).list();
					cha.setBrand(bl.get(0));
				} else if (key.equals(Constant.chaSelectStr[1])) {
					cha.setType(getValueByName(list.get(0)));
				} else if (key.equals(Constant.chaSelectStr[2])) {
					cha.setStructure(getValueByName(list.get(0)));
				} else if (key.equals(Constant.chaSelectStr[3])) {
					cha.setChassisMaterial(getValueByName(list.get(0)));
				} else if (key.equals(Constant.chaSelectStr[4])) {
					cha.setWireFuction(getValueByName(list.get(0)));
				} else if (key.equals(Constant.chaInforStr[1])) {
					// 非外键
					cha.setExtendSlot(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.chaInforStr[2])) {
					cha.setC525(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.chaInforStr[3])) {
					cha.setC35(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.chaInforStr[4])) {
					cha.setC25(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.chaInforStr[5])) {
					cha.setChasissSize(list.get(0));
				} else if (key.equals(Constant.chaInforStr[6])) {
					cha.setChasissWeight(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.chaInforStr[7])) {
					cha.setToolFreeAssemble(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.chaInforStr[8])) {
					cha.setAttachment(list.get(0));
				} else if (key.equals(Constant.chaComboBoxStr[0])) {
					// 一对多
					chaIntf = list;
				} else if (key.equals(Constant.chaComboBoxStr[1])) {
					chaMBA = list;
				} else {
					// 通用属性
					Changer.setUniAttribute(cha, key, list);
				}

			}
			// 默认属性
			Changer.setDefaultAttribute(cha);
			cha.setState(getValueByID(Constant.HW_DEFAULT_STATE));
			
			sess.save(cha);
			// 机箱接口表Cha_Interface
			for (int j = 0; j < chaIntf.size(); j++) {
				String s = chaIntf.get(j);
				int num = s.toCharArray()[0];
				String intfName = s.substring(2);
				Interface intf = getInterfaceByName(intfName);

				Cha_Interface ci = new Cha_Interface();
				ci.setCha(cha);
				ci.setIntf_id(intf);
				ci.setNum(num);
				sess.save(ci);
			}
			// 机箱支持主板架构表Cha_MBArchitect
			for (int j = 0; j < chaMBA.size(); j++) {
				String s = chaMBA.get(j);
				Value v = getValueByName(s);

				Cha_MBArchitect cmba = new Cha_MBArchitect();
				cmba.setCha(cha);
				cmba.setMbArchitect(v);
				sess.save(cmba);
			}
			
			sess.flush();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private List<String> getValueByKey(int i, int j) {
		List<String> rl = new ArrayList<String>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value where KEY>=" + i + " AND KEY<=" + j+" ORDER BY VALUE";
		List<Value> l = sess.createQuery(sql).list();
		for (int k = 0; k < l.size(); k++) {
			rl.add(l.get(k).getValue());
		}
		
		return rl;
	}

	private Value getValueByName(String name) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value WHERE VALUE='" + name + "'";
		// System.out.println(sql);
		List<Value> l = sess.createQuery(sql).list();
		
		return l.get(0);

	}

	private Interface getInterfaceByName(String name) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Interface WHERE NAME='" + name + "'";
		List<Interface> l = sess.createQuery(sql).list();
		
		return l.get(0);

	}

	@Override
	public void save(Chassis t) {
		Session sess = sessionFactory.getCurrentSession();
//		Transaction tran = sess.beginTransaction();
		sess.saveOrUpdate(t);
		sess.flush();
		
	}

	@Override
	public int filterPageNum(Map<String, List<Integer>> map, HardwareSet hs) {
		//先根据hs的情况，获得匹配的表
		//后根据map对匹配的表进行筛选 获得最终匹配表
		List<Chassis> hwList = null;
		List<Chassis> matchList = new ArrayList<Chassis>();
		
		//若hs为空 则取全表进行筛选
		//若hs不为空 则先获得匹配的表
		String sql = "FROM Chassis WHERE STATE=882";
		Session sess = sessionFactory.getCurrentSession();
		hwList = sess.createQuery(sql).list();
		
		hwList = uniFilter(hs, hwList);
		

		if (map==null || map.size()==0)
			return hwList.size();
		// 开始过滤
		Set<String> keys = map.keySet();
		int i = 0;
		for (String s : keys) {
			i++;
			if (s.equals(Constant.chaItemNameList[0])) {
				// 品牌过滤
				List<Integer> list = map.get(s);
				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getBrand().getBrand()
								.equals(getBrandById(list.get(k)).getBrand())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}

			} else if (s.equals(Constant.chaItemNameList[1])) {
				// 价格过滤
				List<Integer> list = map.get(s);
				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer.checkPrice(hwList.get(j),
								Constant.CHA_PRICE[list.get(k) - 1].getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}

				}

			} else if (s.equals(Constant.chaItemNameList[2])) {
				// 机箱类型
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getType().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}

			} else if (s.equals(Constant.chaItemNameList[3])) {
				// 机箱结构
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getStructure().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}

			}
			if (i != keys.size()) {
				hwList = matchList;
				matchList = new ArrayList<Chassis>();
			}
		}
		
		return matchList.size();

	}

	@Override
	public int getListPageNum(HardwareSet hs) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Chassis WHERE STATE=882";
		List<Chassis> list = sess.createQuery(sql).list();
		list = uniFilter(hs, list);
		
		return list.size();
	}

	private List<Chassis> uniFilter(HardwareSet hs, List<Chassis> list) {
		if (hs != null) {
			list = MBFilter(hs, list);
		}
		return list;
	}

	@Override
	public List<Chassis> getOneBySupplierID(int id) {
		// TODO Auto-generated method stub
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Chassis where SUPPLIER="+id;
		List<Chassis> list = sess.createQuery(sql).list();
		
		return list;
	}
}
